﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Company.BLL.WS.KDT.GC;
using System.Data.Common;

namespace Company.BLL.KDT.GC
{
	public partial class DinhMucDangKy
	{
		#region Private members.
		
		protected long _ID;
		protected long _SoTiepNhan;
        protected long _ID_HopDong;
        protected DateTime _NgayTiepNhan = new DateTime(1900, 01, 01);        
		protected int _TrangThaiXuLy;

		#endregion
		
		//---------------------------------------------------------------------------------------------

		#region Properties.
		
		public long ID
		{
			set {this._ID = value;}
			get {return this._ID;}
		}      
		public long SoTiepNhan
		{
			set {this._SoTiepNhan = value;}
			get {return this._SoTiepNhan;}
		}

        public long ID_HopDong
        {
            set { this._ID_HopDong = value; }
            get { return this._ID_HopDong; }
        }

        
        public DateTime NgayTiepNhan
        {
            set { this._NgayTiepNhan = value; }
            get { return this._NgayTiepNhan; }
        }
		public int TrangThaiXuLy
		{
			set {this._TrangThaiXuLy = value;}
			get {return this._TrangThaiXuLy;}
		}
		
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Select methods.
		
		public static DinhMucDangKy Load(long iD)
		{
			string spName = "[dbo].p_KDT_GC_DinhMucDangKy_Load";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@ID", SqlDbType.BigInt, iD);
			DinhMucDangKy entity = null;
            IDataReader reader = db.ExecuteReader(dbCommand);
			if (reader.Read())
			{
				entity = new DinhMucDangKy();
				if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt64(reader.GetOrdinal("ID"));
				if (!reader.IsDBNull(reader.GetOrdinal("SoTiepNhan"))) entity.SoTiepNhan = reader.GetInt64(reader.GetOrdinal("SoTiepNhan"));
                if (!reader.IsDBNull(reader.GetOrdinal("ID_HopDong"))) entity.ID_HopDong = reader.GetInt64(reader.GetOrdinal("ID_HopDong"));
				if (!reader.IsDBNull(reader.GetOrdinal("TrangThaiXuLy"))) entity.TrangThaiXuLy = reader.GetInt32(reader.GetOrdinal("TrangThaiXuLy"));
                if (!reader.IsDBNull(reader.GetOrdinal("NgayTiepNhan"))) entity.NgayTiepNhan = reader.GetDateTime(reader.GetOrdinal("NgayTiepNhan"));
			}
			reader.Close();
			dbCommand.Connection.Close();
			
			return entity;
		}		
		
		//---------------------------------------------------------------------------------------------
		
		public static DataSet SelectBy_HopDong_ID(long idHopDong)
		{
			string spName = "[dbo].p_KDT_GC_DinhMucDangKy_SelectBy_HopDong_ID";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID_HopDong", SqlDbType.BigInt, idHopDong);           	
            return db.ExecuteDataSet(dbCommand);
		}				
		
		//---------------------------------------------------------------------------------------------
		

		//---------------------------------------------------------------------------------------------

        public static IDataReader SelectReaderBy_HopDong_ID(long idHopDong)
		{
            string spName = "[dbo].p_KDT_GC_DinhMucDangKy_SelectBy_HopDong_ID";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID_HopDong", SqlDbType.BigInt, idHopDong);           	
         				
            return db.ExecuteReader(dbCommand);
		}
		
		//---------------------------------------------------------------------------------------------


        public static DinhMucDangKyCollection SelectCollectionBy_HopDong_ID(long idHopDong)
		{
            string spName = "[dbo].p_KDT_GC_DinhMucDangKy_SelectBy_HopDong_ID";
        
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID_HopDong", SqlDbType.BigInt, idHopDong);

            IDataReader reader = db.ExecuteReader(dbCommand);
			DinhMucDangKyCollection collection = new DinhMucDangKyCollection();			
			while (reader.Read())
			{
				DinhMucDangKy entity = new DinhMucDangKy();
				
				if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt64(reader.GetOrdinal("ID"));
				if (!reader.IsDBNull(reader.GetOrdinal("SoTiepNhan"))) entity.SoTiepNhan = reader.GetInt64(reader.GetOrdinal("SoTiepNhan"));
                if (!reader.IsDBNull(reader.GetOrdinal("ID_HopDong"))) entity._ID_HopDong = reader.GetInt64(reader.GetOrdinal("ID_HopDong"));			
				if (!reader.IsDBNull(reader.GetOrdinal("TrangThaiXuLy"))) entity.TrangThaiXuLy = reader.GetInt32(reader.GetOrdinal("TrangThaiXuLy"));
                if (!reader.IsDBNull(reader.GetOrdinal("NgayTiepNhan"))) entity.NgayTiepNhan = reader.GetDateTime(reader.GetOrdinal("NgayTiepNhan"));
				collection.Add(entity);
			}
			reader.Close();
			dbCommand.Connection.Close();
			return collection;			
			
		}
		
		//---------------------------------------------------------------------------------------------
		

		public static DataSet SelectAll()
        {
            string spName = "[dbo].p_KDT_GC_DinhMucDangKy_SelectAll";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			
            return db.ExecuteDataSet(dbCommand);
        }
		
		//---------------------------------------------------------------------------------------------

		public static IDataReader SelectReaderAll()
        {
            string spName = "[dbo].p_KDT_GC_DinhMucDangKy_SelectAll";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			
            return db.ExecuteReader(dbCommand);
        }
		
		//---------------------------------------------------------------------------------------------
		
		public static DinhMucDangKyCollection SelectCollectionAll()
		{
			DinhMucDangKyCollection collection = new DinhMucDangKyCollection();

			IDataReader reader = SelectReaderAll();
			while (reader.Read())
			{
				DinhMucDangKy entity = new DinhMucDangKy();

                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt64(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("SoTiepNhan"))) entity.SoTiepNhan = reader.GetInt64(reader.GetOrdinal("SoTiepNhan"));
                if (!reader.IsDBNull(reader.GetOrdinal("ID_HopDong"))) entity._ID_HopDong = reader.GetInt64(reader.GetOrdinal("ID_HopDong"));
                if (!reader.IsDBNull(reader.GetOrdinal("TrangThaiXuLy"))) entity.TrangThaiXuLy = reader.GetInt32(reader.GetOrdinal("TrangThaiXuLy"));
                if (!reader.IsDBNull(reader.GetOrdinal("NgayTiepNhan"))) entity.NgayTiepNhan = reader.GetDateTime(reader.GetOrdinal("NgayTiepNhan"));
                collection.Add(entity);
			}
			reader.Close();
			return collection;			
		}
		
		//---------------------------------------------------------------------------------------------
		
		public static DataSet SelectDynamic(string whereCondition, string orderByExpression)
		{
            string spName = "[dbo].p_KDT_GC_DinhMucDangKy_SelectDynamic";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);
            
            return db.ExecuteDataSet(dbCommand);        				
		}
		
		//---------------------------------------------------------------------------------------------

		public static IDataReader SelectReaderDynamic(string whereCondition, string orderByExpression)
		{
            string spName = "[dbo].p_KDT_GC_DinhMucDangKy_SelectDynamic";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);
            
            return db.ExecuteReader(dbCommand);        				
		}
		
		//---------------------------------------------------------------------------------------------
				
		public static DinhMucDangKyCollection SelectCollectionDynamic(string whereCondition, string orderByExpression)
		{
			DinhMucDangKyCollection collection = new DinhMucDangKyCollection();

			IDataReader reader = SelectReaderDynamic(whereCondition, orderByExpression);
			while (reader.Read())
			{
				DinhMucDangKy entity = new DinhMucDangKy();
				
				if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt64(reader.GetOrdinal("ID"));
				if (!reader.IsDBNull(reader.GetOrdinal("SoTiepNhan"))) entity.SoTiepNhan = reader.GetInt64(reader.GetOrdinal("SoTiepNhan"));
                if (!reader.IsDBNull(reader.GetOrdinal("ID_HopDong"))) entity._ID_HopDong = reader.GetInt64(reader.GetOrdinal("ID_HopDong"));
                if (!reader.IsDBNull(reader.GetOrdinal("NgayTiepNhan"))) entity.NgayTiepNhan = reader.GetDateTime(reader.GetOrdinal("NgayTiepNhan"));
				if (!reader.IsDBNull(reader.GetOrdinal("TrangThaiXuLy"))) entity.TrangThaiXuLy = reader.GetInt32(reader.GetOrdinal("TrangThaiXuLy"));
				collection.Add(entity);
			}
			reader.Close();
			return collection;			
		}
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Insert methods.

        public static long Insert(long soTiepNhan, long ID_HopDong, int trangThaiXuLy, DateTime ngaytiepnhan)
		{
			DinhMucDangKy entity = new DinhMucDangKy();			
			entity.SoTiepNhan = soTiepNhan;
            entity.TrangThaiXuLy = trangThaiXuLy;
            entity.ID_HopDong = ID_HopDong; ;
            entity._NgayTiepNhan = ngaytiepnhan;
			long returnID = entity.Insert();
						
			return returnID;
		}
		
		public bool Insert(DinhMucDangKyCollection collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection) db.CreateConnection())
            {
                connection.Open();
                SqlTransaction transaction = connection.BeginTransaction();
                try
                {
					bool ret01 = true;
                    foreach (DinhMucDangKy item in collection)
                    {
                        if (item.InsertTransaction(transaction) <= 0)
						{							
							ret01 = false;
							break;
						}
                    }
					if (ret01)
					{
                    	transaction.Commit();
                    	ret = true;
					}
					else
					{
                    	transaction.Rollback();
						ret = false;                    	
					}
                }
                catch
                {
                    ret = false;
                    transaction.Rollback();
                }
                finally 
                {
                    connection.Close();
                }
            }
            return ret;		
		}
		
		public long Insert()
		{
			return this.InsertTransaction(null);
		}		
		
		//---------------------------------------------------------------------------------------------
		
		public long InsertTransaction(SqlTransaction transaction)
		{			
			string spName = "[dbo].p_KDT_GC_DinhMucDangKy_Insert";		
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddOutParameter(dbCommand, "@ID", SqlDbType.BigInt, 8);
			db.AddInParameter(dbCommand, "@SoTiepNhan", SqlDbType.BigInt, this._SoTiepNhan);
            db.AddInParameter(dbCommand, "@ID_HopDong", SqlDbType.BigInt, this._ID_HopDong);
            db.AddInParameter(dbCommand, "@TrangThaiXuLy", SqlDbType.Int, this._TrangThaiXuLy);
            db.AddInParameter(dbCommand, "@NgayTiepNhan", SqlDbType.DateTime, this._NgayTiepNhan);         
			if (transaction != null)
			{
				db.ExecuteNonQuery(dbCommand, transaction);				
			}
            else
			{
				db.ExecuteNonQuery(dbCommand);				
			}
            this._ID = (long)db.GetParameterValue(dbCommand, "@ID");
            return this._ID;
		}
		
		//---------------------------------------------------------------------------------------------

        public static bool insertDinhMucAndDinhMucDangKy(DinhMucCollection dmCollection, DinhMucDangKy dmdk)
        {
            bool ret;
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                SqlTransaction transaction = connection.BeginTransaction();
                try
                {
                    bool ret01 = true;
                    long id = dmdk.InsertTransaction(transaction);
                    if(id>0)
                    foreach (DinhMuc item in dmCollection)
                    {
                        item.Master_ID = id;
                        if (item.InsertTransaction(transaction) <= 0)
                        {
                            ret01 = false;
                            break;
                        }
                    }
                    if (ret01)
                    {
                        transaction.Commit();
                        ret = true;
                    }
                    else
                    {
                        transaction.Rollback();
                        ret = false;
                    }
                }
                catch
                {
                    ret = false;
                    transaction.Rollback();
                }
                finally
                {
                    connection.Close();
                }
            }
            return ret;	
        }
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Insert / Update methods.
        //-----------------------------------------------------
        /// <summary>
        /// insert update thong tin cua dinh muc va collection
        /// </summary>
        /// <returns></returns>
        public static bool InsertUpdate(DinhMucDangKy dmDangKy, DinhMucCollection dmCollection)
        {
            bool ret;
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                SqlTransaction transaction = connection.BeginTransaction();
                try
                {
                    bool ret01 = true;
                    if (dmDangKy.InsertUpdateTransaction(transaction) <= 0)
                    {
                        ret01 = false;
                    }
                    else
                    {
                        foreach (DinhMuc item in dmCollection)
                        {
                            item.Master_ID = dmDangKy.ID;
                            if (item.InsertUpdateTransaction(transaction) <= 0)
                            {
                                ret01 = false;
                                break;
                            }
                        }
                    }
                    if (ret01)
                    {
                        transaction.Commit();
                        ret = true;
                    }
                    else
                    {
                        transaction.Rollback();
                        ret = false;
                    }
                }
                catch
                {
                    ret = false;
                    transaction.Rollback();
                }
                finally
                {
                    connection.Close();
                }
            }
            return ret;
        }		
        //-----------------------------------------------------
		public int InsertUpdate()
		{
			return this.InsertUpdateTransaction(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int InsertUpdateTransaction(SqlTransaction transaction)
		{			
			string spName = "p_KDT_GC_DinhMucDangKy_InsertUpdate";	
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			db.AddInParameter(dbCommand, "@ID", SqlDbType.BigInt, this._ID);
			db.AddInParameter(dbCommand, "@SoTiepNhan", SqlDbType.BigInt, this._SoTiepNhan);
            db.AddInParameter(dbCommand, "@ID_HopDong", SqlDbType.BigInt, this._ID_HopDong);
            db.AddInParameter(dbCommand, "@TrangThaiXuLy", SqlDbType.Int, this._TrangThaiXuLy);
            db.AddInParameter(dbCommand, "@NgayTiepNhan", SqlDbType.DateTime, this._NgayTiepNhan);         
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);			
		}
		
		//---------------------------------------------------------------------------------------------
		
		public bool InsertUpdate(DinhMucDangKyCollection collection)
        {
            bool ret;			
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection) db.CreateConnection())
            {
                connection.Open();
                SqlTransaction transaction = connection.BeginTransaction();
                try
                {
					bool ret01 = true;
                    foreach (DinhMucDangKy item in collection)
                    {
                        if (item.InsertUpdateTransaction(transaction) <= 0)
						{
							ret01 = false;
							break;
						}
                    }
					if (ret01)
					{
						transaction.Commit();
						ret = true;
					}
					else
					{
						transaction.Rollback();
						ret = false;                    	
					}
                }
                catch
                {
                    ret = false;
                    transaction.Rollback();
                }
                finally 
                {
                    connection.Close();
                }
            }
            return ret;		
		}		
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Update methods.

        public static int Update(long iD, long soTiepNhan, long id_HopDong, int trangThaiXuLy, DateTime ngaytiepnhan)
		{
			DinhMucDangKy entity = new DinhMucDangKy();			
			entity.ID = iD;
			entity.SoTiepNhan = soTiepNhan;
            entity.ID_HopDong = id_HopDong;
            entity.NgayTiepNhan = ngaytiepnhan;
			return entity.Update();
		}
		
		public int Update()
		{
			return this.UpdateTransaction(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public bool Update(DinhMucDangKyCollection collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection) db.CreateConnection())
            {
                connection.Open();
                SqlTransaction transaction = connection.BeginTransaction();
                try
                {
					bool ret01 = true;
                    foreach (DinhMucDangKy item in collection)
                    {
                        if (item.UpdateTransaction(transaction) <= 0)
						{							
							ret01 = false;
							break;
						}
                    }
					if (ret01)
					{
                    	transaction.Commit();
                    	ret = true;
					}
					else
					{
                    	transaction.Rollback();
						ret = false;                    	
					}
                }
                catch
                {
                    ret = false;
                    transaction.Rollback();
                }
                finally 
                {
                    connection.Close();
                }
            }
            return ret;		
		}

		public int UpdateTransaction(SqlTransaction transaction)
		{
			string spName = "[dbo].p_KDT_GC_DinhMucDangKy_Update";		
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@ID", SqlDbType.BigInt, this._ID);
			db.AddInParameter(dbCommand, "@SoTiepNhan", SqlDbType.BigInt, this._SoTiepNhan);
            db.AddInParameter(dbCommand, "@ID_HopDong", SqlDbType.BigInt, this._ID_HopDong);
			db.AddInParameter(dbCommand, "@TrangThaiXuLy", SqlDbType.Int, this._TrangThaiXuLy);
            db.AddInParameter(dbCommand, "@NgayTiepNhan", SqlDbType.DateTime, this._NgayTiepNhan);         
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
		}
		
		//---------------------------------------------------------------------------------------------
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Delete methods.
		
		public static int Delete(long iD)
		{
			DinhMucDangKy entity = new DinhMucDangKy();
			entity.ID = iD;
			return entity.Delete();
		}
		
		public int Delete()
		{
			return this.DeleteTransaction(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public bool DeleteCollection(DinhMucDangKyCollection collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection) db.CreateConnection())
            {
                connection.Open();
                SqlTransaction transaction = connection.BeginTransaction();
                try
                {
					bool ret01 = true;
                    foreach (DinhMucDangKy item in collection)
                    {
                        if (item.DeleteTransaction(transaction) <= 0)
						{
							ret01 = false;
							break;
						}
                    }
					if (ret01)
					{
						transaction.Commit();
						ret = true;
					}
					else
					{
						transaction.Rollback();
						ret = false;                    	
					}
                }
                catch
                {
                    ret = false;
                    transaction.Rollback();
                }
                finally 
                {
                    connection.Close();
                }
            }
            return ret;		
		}

		//---------------------------------------------------------------------------------------------

		public static void DeleteCollection(DinhMucDangKyCollection collection, SqlTransaction transaction)
		{			
            foreach (DinhMucDangKy item in collection)
            {
                item.DeleteTransaction(transaction);
            }			
		}

		public int DeleteTransaction(SqlTransaction transaction)
		{
			string spName = "[dbo].p_KDT_GC_DinhMucDangKy_Delete";		
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@ID", SqlDbType.BigInt, this._ID);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
		}
				
		#endregion

        //---------------------------------------------------------------------------------------------

        #region CheckExitDinhMuc

        public bool checkExitDinhMuc(string manpl,string masp)
        {
          	SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();		
            string sql= "select dm.id "+
                        "from t_KDT_GC_DinhMuc dm "+
                        "inner join t_KDT_GC_DinhMucDangKy dmdk on dmdk.ID=dm.Master_ID "+
                        "where dmdk.id <> @id and ID_HopDong=@ID_HopDong";
            DbCommand com=db.GetSqlStringCommand(sql);
            db.AddInParameter(com,"@id",SqlDbType.BigInt,this.ID);
            db.AddInParameter(com, "@ID_HopDong", SqlDbType.BigInt, this.ID_HopDong);
            IDataReader reader = db.ExecuteReader(com);
            bool exit=false;
            if (reader.Read())
                exit = true;
            return exit;
        }



        #endregion CheckExitDinhMuc

        //---------------------------------------------------------------------------------------------
	

        #region WebService

        private DinhMucInfo[] ConvertToDinhMucInfoCollection()
        {
            DinhMucCollection dmCollection = DinhMuc.SelectCollectionBy_Master_ID(this.ID);
            int i = 0;
            HopDong hd = HopDong.Load(this.ID_HopDong);
            DinhMucInfo[] dmInfoCollection = new DinhMucInfo[dmCollection.Count];
            foreach (DinhMuc dm in dmCollection)
            {
                DinhMucInfo dmInfo = new DinhMucInfo();
                dmInfo.DinhMuc = dm.DinhMucSuDung;
                dmInfo.DVT_ID = dm.DVT_ID;
                dmInfo.Ghi_Chu = dm.GhiChu;
                dmInfo.MaDoanhNghiep = hd.MaDoanhNghiep;
                dmInfo.MaHaiQuan = hd.MaHaiQuan;
                dmInfo.MaNPL = dm.MaNguyenPhuLieu;
                dmInfo.MaSP = dm.MaSanPham;
                dmInfo.MuaNPL_VN = dm.NPL_TuCungUng;
                dmInfo.NamTiepNhan = this.NgayTiepNhan.Year;
                dmInfo.NgayKy = hd.NgayKy;
                dmInfo.NgayTiepNhan = this.NgayTiepNhan;
                dmInfo.SoHopDong = hd.SoHopDong;
                dmInfo.SoTiepNhan = this.SoTiepNhan;
                dmInfo.STTHang = dm.STTHang;
                dmInfo.TenNPL = dm.TenNPL;
                dmInfo.TenSP = dm.TenSanPham;
                dmInfo.TrangThaiXL = this.TrangThaiXuLy;
                dmInfo.TyLeHaoHut = dm.TyLeHaoHut;
                dmInfo.DVT_ID = dm.DVT_ID;
                dmInfoCollection[i++] = dmInfo;
            }
            return dmInfoCollection;
        }

        public void WSSend()
        {


            DinhMucInfo[] dmInfoCollection = ConvertToDinhMucInfoCollection();
            KDT_GC_Service kdt_gc = new KDT_GC_Service();
            this.SoTiepNhan = kdt_gc.DinhMuc_Send(dmInfoCollection);
            this.NgayTiepNhan = DateTime.Today;
            this.TrangThaiXuLy = 0;
            this.InsertUpdate();
        }
        public bool WSCancel()
        {
            KDT_GC_Service kdt_gc = new KDT_GC_Service();
            DinhMucInfo dmInfo=new DinhMucInfo();
            dmInfo.SoTiepNhan=this.SoTiepNhan;
            dmInfo.MaHaiQuan=HopDong.Load(this._ID_HopDong).MaHaiQuan;
            dmInfo.NamTiepNhan=this.NgayTiepNhan.Year;
            bool kq=kdt_gc.DinhMuc_Cancel(dmInfo);
            this.TrangThaiXuLy = -1;
            this.SoTiepNhan = 0;
            this.NgayTiepNhan = new DateTime(1900, 1, 1);
            this.InsertUpdate();
            return kq;
        }

        public void  WSRequest()
        {
            KDT_GC_Service kdt_gc = new KDT_GC_Service();
            DinhMucInfo[] dmInfoCollection = kdt_gc.DinhMuc_RequestStatus(this.SoTiepNhan, HopDong.Load(this._ID_HopDong).MaHaiQuan, (short)this.NgayTiepNhan.Year);
            this.TrangThaiXuLy = dmInfoCollection[0].TrangThaiXL;            
              bool ret=true;			
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection) db.CreateConnection())
            {
                connection.Open();
                SqlTransaction transaction = connection.BeginTransaction();
                try
                {
                    this.InsertUpdateTransaction(transaction);
                    DinhMuc.DeleteByMasterID(transaction,this.ID);
                    foreach (DinhMucInfo dmInfo in dmInfoCollection)
                    {
                        DinhMuc dm = new DinhMuc();
                        dm.DinhMucSuDung = dmInfo.DinhMuc;
                        dm.DVT_ID = dmInfo.DVT_ID;
                        dm.GhiChu = dmInfo.Ghi_Chu;
                        dm.MaNguyenPhuLieu = dmInfo.MaNPL;
                        dm.MaSanPham = dmInfo.MaSP;
                        dm.Master_ID = this.ID;
                        dm.NPL_TuCungUng = dmInfo.MuaNPL_VN;
                        dm.STTHang = dmInfo.STTHang;
                        dm.TyLeHaoHut = dmInfo.TyLeHaoHut;
                        dm.TenSanPham = dmInfo.TenSP;
                        dm.TenNPL = dmInfo.TenNPL;
                        dm.InsertTransaction(transaction);
                        if (this.TrangThaiXuLy == 1)
                        {
                            Company.BLL.GC.DinhMuc dmDuyet= new Company.BLL.GC.DinhMuc();
                            dmDuyet.DinhMucSuDung = dmInfo.DinhMuc;
                            dmDuyet.GhiChu = dmInfo.Ghi_Chu;
                            dmDuyet.MaDoanhNghiep = dmInfo.MaDoanhNghiep;
                            dmDuyet.MaHaiQuan = dmInfo.MaHaiQuan;
                            dmDuyet.MaNguyenPhuLieu = dmInfo.MaNPL;
                            dmDuyet.MaSanPham = dmInfo.MaSP;
                            dmDuyet.NgayKy = dmInfo.NgayKy;
                            dmDuyet.NPL_TuCungUng = dmInfo.MuaNPL_VN;
                            dmDuyet.SoHopDong = dmInfo.SoHopDong;
                            dmDuyet.TrangThaiXuLy = 1;
                            dmDuyet.TyLeHaoHut = dmInfo.TyLeHaoHut;
                            dmDuyet.TenNPL = dmInfo.TenNPL;
                            dmDuyet.TenSP = dmInfo.TenSP;
                            dmDuyet.DVT_ID = dmInfo.DVT_ID;
                            dmDuyet.InsertTransaction(transaction);
                        }
                    }
                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    throw ex;
                }
                finally
                {
                    connection.Close();
                }
            }
         

        }

        #endregion WebService
    }	
}